<template>
	<div>
		<md-toolbar class="md-dense" md-elevation="0">
			<span class="md-body-1">{{getLangText(label.grid)}}</span>
			<span class="md-toolbar-section-end">
				<!-- <md-button class="md-icon-button" @click="sendStyle" v-show="isShow">
					<i class="iconfont iconsave"></i>
				</md-button> -->
				<md-button class="md-icon-button" @click="sendStyle">
					<i :class="['iconfont',isShow?'iconsave':'iconright']"></i>
				</md-button>
			</span>
		</md-toolbar>
		<div v-if="isShow" style="padding:0 8px;">
			<div class="table-row-item">
				<span>{{getLangText(label.grid)}}{{getLangText(label.show)}}:</span>
				<span>
					{{getLangText(label.hide)}}
					<md-switch v-model="grid.show"></md-switch>
					{{getLangText(label.show)}}
				</span>
			</div>
			<div class="table-row-item">
				<span>{{getLangText(label.containLabel)}}:</span>
				<span>
					{{getLangText(label.no)}}
					<md-switch v-model="grid.containLabel"></md-switch>
					{{getLangText(label.yes)}}
				</span>
			</div>
			<md-field>
				<label>{{getLangText(label.left)}}</label>
				<md-input v-model="grid.left"></md-input>
			</md-field>

			<md-field>
				<label>{{getLangText(label.top)}}</label>
				<md-input v-model="grid.top"></md-input>
			</md-field>

			<md-field>
				<label>{{getLangText(label.right)}}</label>
				<md-input v-model="grid.right"></md-input>
			</md-field>

			<md-field>
				<label>{{getLangText(label.bottom)}}</label>
				<md-input v-model="grid.bottom"></md-input>
			</md-field>

			<div class="table-row-item">
				<span>{{getLangText(label.bg)}}:</span>
				<span>
					<span
						class="color-span"
						:style="{background:grid.backgroundColor}"
						@click="showColorPicker('backgroundColor')"
					></span>
					{{grid.backgroundColor}}
				</span>
			</div>
			<color-picker
				:lang-type="langType"
				:rgba="grid.backgroundColor"
				@color-result="onColorPicker"
				:show-picker="picker.show&&picker.current=='backgroundColor'"
			></color-picker>

			<md-field>
				<label>{{getLangText(label.borderWidth)}}</label>
				<md-input v-model="grid.borderWidth"></md-input>
			</md-field>

			<div class="table-row-item">
				<span>{{getLangText(label.borderColor)}}:</span>
				<span>
					<span
						class="color-span"
						:style="{background:grid.borderColor}"
						@click="showColorPicker('borderColor')"
					></span>
					{{grid.borderColor}}
				</span>
			</div>

			<color-picker
				:lang-type="langType"
				:rgba="grid.borderColor"
				@color-result="onColorPicker"
				:show-picker="picker.show&&picker.current=='borderColor'"
			></color-picker>

			<md-field>
				<label>{{getLangText(label.shadowBlur)}}</label>
				<md-input v-model.number="grid.shadowBlur"></md-input>
			</md-field>

			<div class="table-row-item">
				<span>{{getLangText(label.shadowColor)}}:</span>
				<span>
					<span
						class="color-span"
						:style="{background:grid.shadowColor}"
						@click="showColorPicker('shadowColor')"
					></span>
					{{grid.shadowColor}}
				</span>
			</div>

			<color-picker
				:lang-type="langType"
				:rgba="grid.shadowColor"
				@color-result="onColorPicker"
				:show-picker="picker.show&&picker.current=='shadowColor'"
			></color-picker>

			<md-field>
				<label>{{getLangText(label.shadowOffsetX)}}</label>
				<md-input v-model.number="grid.shadowOffsetX"></md-input>
			</md-field>
			<md-field>
				<label>{{getLangText(label.shadowOffsetY)}}</label>
				<md-input v-model.number="grid.shadowOffsetY"></md-input>
			</md-field>
		</div>
	</div>
</template>
<script>
	import ColorPicker from "@/components/comp/ColorPicker";
	export default {
		name: "Grid",
		props: {
			langType: {
				type: String,
				default: window.localStorage.getItem("langType")
			},
			option: {
				type: Array,
				default: function() {
					return [];
				}
			}
		},
		components: {
			"color-picker": ColorPicker
		},
		data: () => ({
			label: {
				grid: { zh: "直角坐标系", en: "grid" },
				shadowBlur: { zh: "阴影模糊大小", en: "shadowBlur" },
				shadowColor: { zh: "阴影颜色", en: "shadowColor" },
				shadowOffsetX: { zh: "阴影水平偏移", en: "shadowOffsetX" },
				shadowOffsetY: { zh: "阴影垂直偏移", en: "shadowOffsetY" },
				show: { zh: "显示", en: "show" },
				hide: { zh: "隐藏", en: "hide" },
				yes: { zh: "是", en: "yes" },
				no: { zh: "否", en: "no" },
				left: { zh: "左侧距离", en: "left" },
				top: { zh: "上侧距离", en: "top" },
				right: { zh: "右侧距离", en: "right" },
				bottom: { zh: "下侧距离", en: "bottom" },
				containLabel: { zh: "包含刻度标签", en: "containLabel" },
				bg: { zh: "背景颜色", en: "bgColor" },
				borderColor: { zh: "边框颜色", en: "borderColor" },
				borderWidth: { zh: "边框线宽", en: "borderWidth" }
			},
			isShow: false,
			grid: {
				show: false,
				left: "10%",
				top: 60,
				right: "10%",
				bottom: 60,
				width: "auto",
				height: "auto",
				containLabel: false,
				backgroundColor: "transparent",
				borderColor: "#ccc",
				borderWidth:0,
				shadowBlur: "",
				shadowColor: "",
				shadowOffsetX: 0,
				shadowOffsetY: 0
			},
			picker: {
				show: false,
				current: ""
			},
			originOption:'',
			isParse:false
		}),
		watch:{
			isShow() {
				if (this.isShow) {
					this.parseObj();					
				}else{
					this.picker.show = false;
				}

			},
			grid:{
				deep:true,
				handler(){
					if(this.isShow&&!this.isParse)
					this.$emit('grid-result',JSON.parse(JSON.stringify([this.grid])));
				}
			}
		},
		methods: {
			showColorPicker(tag) {
				if (tag != this.picker.current || !this.picker.show) {
					this.picker.current = tag;
					this.picker.show = true;
				} else {
					this.picker.show = false;
				}
			},
			onColorPicker(op) {
				if(this.picker.current)
				this.grid[this.picker.current]=op.rgba;
			},
			sendStyle() {
				this.isShow=this.isShow?false:true;
				if(!this.isShow){
				this.$emit('grid-result',JSON.parse(JSON.stringify([this.grid])));
				}
			},
			parseObj(){
				if(this.option instanceof Array&&this.option.length==1){
					this.isParse=true;
					var option=this.option[0];
				for(var key in option){
					this.grid[key]=option[key];
				}
				this.originOption=JSON.stringify(this.grid);
				var that=this;
				setTimeout(() => {
					that.isParse=false;
				}, 100);
				}
			},
		colseOption(){
			this.isShow=false;
			var that=this;
			if(this.originOption!=JSON.stringify(this.grid)){
			setTimeout(function(){
				that.$emit('grid-result',[that.grid]);
			},50)
			
			}
			},
			listenEvent(){
				this.app.$on('close-option',this.colseOption);
			},
			offEvent(){
			this.app.$off('close-option',this.colseOption);
			}
		},
		mounted() {
			this.listenEvent();
			
		},
		beforeDestroy(){
			this.offEvent();
		}
	};
</script>


